﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Revis.VClinicNet.Core.Domain
{
    public class Time : IComparable<Time>
    {
        public virtual TimeSpan TimeDuringDay { get; protected set; }

        public virtual int Hour { get { return TimeDuringDay.Hours; } }
        public virtual int Minute { get { return TimeDuringDay.Minutes; } }

        private IDateTimeService _date = null;

        #region Constructors

        public Time() :
            this(0,0,null)
        {
        }

        public Time(DateTime date) :
            this(date.Hour, date.Minute, null)
        {            
        }

        public Time(int hour=0, int minute=0) :
            this(hour, minute, null)
        {            
        }

        public Time(int hour, int minute, IDateTimeService dateService)
        {
            if (hour >= 24 || hour < 0 || minute >= 60 || minute < 0) {
                throw new ArgumentException("Niepoprawna godzina");
            }
            TimeDuringDay = new TimeSpan(hour, minute, 0);
            _date = dateService ?? new ActualDateTimeService();
        }

        #endregion

        #region Public Methods

        public DateTime DateTime()
        {
            DateTime baseDate = _date.Now();
            return new DateTime(
                baseDate.Year,
                baseDate.Month,
                baseDate.Day,
                Hour, Minute, 0);
        }

        public DateTime DateTime(DateTime baseDate)
        {
            return new DateTime(
                baseDate.Year, 
                baseDate.Month, 
                baseDate.Day, 
                Hour, Minute, 0);
        }

        public int CompareTo(Time other)
        {
            DateTime baseDate = _date.Now().Date;
            DateTime thisDate = this.DateTime(baseDate);
            DateTime otherDate = other.DateTime(baseDate);
            return thisDate.CompareTo(otherDate);
        }

        #endregion
    }
}
